var api = require("../../utils/api");
Page({
  // noHave
  /**
   * 页面的初始数据
   */
  data: {
    cityNumber: {},//城市电话
    phoneNumber: {},//经理电话
    //搜索数据
    searchRecord: [],//显示
    // 搜索记录最多显示条数
    searchNum: 10,
    resval: "",
    backVal: null,
    // inputShowed:true,
  },
  // 首页打电话
  callGetPhone: function () {
    wx.navigateTo({
      url: '/pages/kefu/kefu',
    })
  },
  onShow: function () {
    this.openHistorySearch();
  },
  onLoad: function (options) {
    let that = this
    // 打电话（城市电话  客户经理电话）-----------------------
    api.phoneList({
      id: wx.getStorageSync("cityId")
    }).then(res => {
      if (res.result) {
        that.setData({
          cityNumber: res.body.cityInfo,
          phoneNumber: res.body.customerManager,
        })
      }
    })
  },

  // 加载
  openHistorySearch() {
    this.setData({
      inputShowed: true,
      searchRecord: wx.getStorageSync('searchRecord') || []
    })
  },
  // 搜索按钮
  searchBtn(e) {
    let { searchRecord, searchNum } = this.data;
    let inputVal = e.detail.value;
    //为空和输入空格
    if (inputVal.replace(/(^\s*)|(\s*$)/g, "") == "") {
      console.log("输入为空");
    } else {
      //正常搜索
      if (searchRecord.length < searchNum) {
        searchRecord.unshift(inputVal)
      } else {
        searchRecord.pop()
        searchRecord.unshift(inputVal)
      }
      // 调用公共

      this.searchIndex(e.detail.value);
    }
  },
  // 搜索过的
  handleSearchRod(e) {
    console.log(e)
    //调用公共
    let val = e.currentTarget.dataset.item;
    this.searchIndex(val);
  },
  // 搜索(公共)
  searchIndex(vals) {
    let that = this;
    let searchRecord = that.data.searchRecord;
    let temp = {}, arr = [], len = searchRecord.length;
    for (var i = 0; i < len; i++) {
      if (!temp[searchRecord[i]]) {//加入i=0 this[o]=arr1[0]=1
        temp[searchRecord[i]] = vals;
        arr.push(searchRecord[i]);//this指的是调用它的这个变量(对象)
      }
    }
    wx.setStorageSync('searchRecord', arr);
    wx.navigateTo({
      url: '/pages/search/search?inputVal=' +vals,
    })
  },
  //搜索历史
  handleSearchGuo() {
    let that = this;
    wx.showModal({
      title: '提示',
      content: '是否删除全部历史记录',
      success(res) {
        if (res.confirm) {
          that.setData({
            searchRecord: []
          })
          wx.removeStorageSync("searchRecord")
        }
      }
    })
  },
  // 搜索栏
  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },
  hideInput: function () {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  clearInput: function () {
    wx.hideKeyboard({
      complete: res => {
        this.setData({
          backVal: ""
        });
      }
    })
  },
  // 点击搜索框
  inputTyping: function (e) {
    this.setData({
      backVal: e.detail.value
    });
  }
})